import cgi
import datetime
import urllib
import webapp2

import jinja2
import os
import datetime
from google.appengine.ext import db

dir = os.path.dirname(__file__)
filename = os.path.join(dir, '../view')
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(filename))

from google.appengine.ext import db

from model.Person import Person
from model.User import User

class Registration(webapp2.RequestHandler):

    def get(self):
        # print 'pepe'
        # curr_fb_uid = int(self.request.get('fb_uid'))
        # obj = User(user_id = 1, gender = 'male', searching = 'female', lastmatch = datetime.date(year=1986, month=3, day=5), confidence = 3.25, fb_uid = 88, fb_current_city = 'Bs As', fb_birthdate = datetime.date(year=1986, month=3, day=5))
        # obj.put()
        # current_user = User.all().filter('fb_uid =', curr_fb_uid).fetch(1)
        # if current_user:
        #     print current_user
        #     people_query = Person.all()
        #     people = people_query.fetch(8)
        #     # mandar la gente por parametro de la pagina
        #     template_values = {
        #         'people': people,
        #     }

        #     template = jinja_environment.get_template('list1.html')
        #     self.response.out.write(template.render(template_values))
        # else:
        #     template = jinja_environment.get_template('registration.html')
        #     self.response.out.write(template.render())
        template = jinja_environment.get_template('registration.html')
        self.response.out.write(template.render())


    def post(self):
        curr_fb_uid = int(self.request.get('fb_uid'))
        obj = User(user_id = 1, gender = 'male', searching = 'female', lastmatch = datetime.date(year=1986, month=3, day=5), confidence = 3.25, fb_uid = 88, fb_current_city = 'Bs As', fb_birthdate = datetime.date(year=1986, month=3, day=5))
        obj.put()
        current_user = User.all().filter('fb_uid =', curr_fb_uid).fetch(1)
        if current_user:
            print current_user
            people_query = Person.all()
            people = people_query.fetch(8)
            # mandar la gente por parametro de la pagina
            template_values = {
                'people': people,
            }

            template = jinja_environment.get_template('list1.html')
            self.response.out.write(template.render(template_values))
        else:
            template = jinja_environment.get_template('registration.html')
            self.response.out.write(template.render())
